<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>添加会员信息画面</title>
    <!-- 引入 Bootstrap CSS -->
    <link href="/css/bootstrap.min.css" rel="stylesheet">
    <link href="/css/update_insert.css" rel="stylesheet">
</head>
<body>
<div class="container my-5">

    <!-- 表单 -->
    <form class="card p-4 shadow-sm" id="memberForm" th:action="@{/userController/add}" method="post">
        <h1 class="text-center mb-4">添加会员信息</h1>
        <div class="mb-3 row">
            <label for="memberId" class="col-sm-2 col-form-label">会员编号</label>
            <div class="col-sm-6">
                <input type="text" class="form-control" id="memberId" name="userId" placeholder="请输入4位会员编号" required>
                <span class="text-danger mt-1" id="idError" style="display: none;">* 会员编号必须为4位数字！</span>
            </div>
        </div>

        <div class="mb-3 row">
            <label for="memberName" class="col-sm-2 col-form-label">名字</label>
            <div class="col-sm-6">
                <input type="text" class="form-control" id="memberName" name="userName" placeholder="请输入名字" required>
                <span class="text-danger mt-1" id="nameError" style="display: none;">* 名字不能为空！</span>
            </div>
        </div>

        <div class="mb-3 row">
            <label for="memberScore" class="col-sm-2 col-form-label">积分</label>
            <div class="col-sm-6">
                <input type="number" class="form-control" id="memberScore" name="userScore" placeholder="请输入积分" required>
                <span class="text-danger mt-1" id="scoreError" style="display: none;">* 积分必须为数字且大于 0！</span>
            </div>
        </div>

        <div class="mb-3 row">
            <label for="memberPhone" class="col-sm-2 col-form-label">电话</label>
            <div class="col-sm-6">
                <input type="text" class="form-control" id="memberPhone" name="userTel" placeholder="请输入手机号" required>
                <span class="text-danger mt-1" id="phoneError" style="display: none;">* 手机号必须为11位数字！</span>
            </div>
        </div>

        <div class="mb-3 row">
            <label class="col-sm-2 col-form-label">性别</label>
            <div class="col-sm-6">
                <div class="form-check form-check-inline">
                    <input class="form-check-input" type="radio" name="userSex" id="male" value="男" checked>
                    <label class="form-check-label" for="male">男</label>
                </div>
                <div class="form-check form-check-inline">
                    <input class="form-check-input" type="radio" name="userSex" id="female" value="女">
                    <label class="form-check-label" for="female">女</label>
                </div>
            </div>
        </div>

        <div class="text-center mt-4">
            <button th:action="@{/userController/doAdd}" type="submit" class="btn btn-primary">添加</button>
            <a th:href="@{/userController/doSearch}" class="btn btn-link">首页</a>
        </div>
    </form>
    <div style="text-align: center">
        <span th:text="${errorMsg}" style="color: white"></span>
    </div>
</div>

<!-- 引入 Bootstrap JS -->
<script src="/js/bootstrap.bundle.min.js"></script>
<script src="/js/jquery-3.6.4.min.js"></script>
<script>
    $(document).ready(function () {
        // 校验功能
        let formValid = true;
        $('#memberForm').on('submit', function (e) {
            let isValid = true;

            // 校验会员编号
            const id = $('#memberId').val();
            if (!/^\d{4}$/.test(id)) {
                $('#idError').show();
                isValid = false;
            } else {
                $('#idError').hide();
            }

            // 校验名字
            const name = $('#memberName').val().trim();
            if (name === '') {
                $('#nameError').show();
                isValid = false;
            } else {
                $('#nameError').hide();
            }

            // 校验积分
            const score = $('#memberScore').val();
            if (!/^\d+$/.test(score) || parseInt(score) <= 0) {
                $('#scoreError').show();
                isValid = false;
            } else {
                $('#scoreError').hide();
            }

            // 校验电话
            const phone = $('#memberPhone').val();
            if (!/^1\d{10}$/.test(phone)) {
                $('#phoneError').show();
                isValid = false;
            } else {
                $('#phoneError').hide();
            }

            // 如果验证失败，取消表单提交
            if (!isValid) {
                e.preventDefault();
            }
        });
    });

</script>
</body>
</html>
